﻿using System;
using System.Configuration;
namespace YPF.DBUtility
{
    /// <summary>
    /// 连接字符串获取类
    /// </summary>
    public class PubConstant
    {
        /*配置文件中写法：
         * <!-- 连接字符串是否加密 -->
         * <add key="ConStringEncrypt" value="false"/>
         * <add key="ConnectionString" value="server=127.0.0.1;database=codematic;uid=sa;pwd=1"/>
        */


        #region 获取连接字符串

        /// <summary>
        /// 获取连接字符串（加密或非加密）
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                string _connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
                if (ConStringEncrypt == "true")
                {
                    _connectionString = DESEncrypt.Decrypt(_connectionString);//解密后的字符串
                }
                return _connectionString;
            }
        }

        #endregion


        #region 得到web.config里配置项的数据库连接字符串

        /// <summary>
        /// 得到web.config里配置项的数据库连接字符串。
        /// </summary>
        /// <param name="configName">配置文件中指定的连接字符串名</param>
        /// <returns></returns>
        public static string GetConnectionString(string configName)
        {
            string connectionString = ConfigurationManager.AppSettings[configName];
            string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
            if (ConStringEncrypt == "true")
            {
                connectionString = DESEncrypt.Decrypt(connectionString);//解密后字符串
            }
            return connectionString;
        }

        #endregion



    }
}
